<?php


class PostAction extends BaseAction {
	public function index(){
		import('ORG.Util.Page');
		$db = M('Post');
		$count = $db->where('is_del = 0')->count();
		$Page  = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$this->list = $db->field("blg_cate.c_name,blg_post.click,blg_post.keyword,blg_post.sort as b_sort,blg_post.id as bid,blg_post.title as btitle,blg_post.add_time as batime,blg_cate.id as cate_id")->where('is_del = 0')->join("blg_cate ON blg_post.c_id = blg_cate.id")->order('blg_post.sort asc,blg_post.add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->page = $Page->show();
		$this->display();
	}

	public function addUi(){
        import('Class.Cate',APP_PATH);
        $cdb = new CateModel();
        $list = $cdb->order('sort asc')->select();
        $list = Cate::unlimitedForLevel($list);
        $this->assign('cates', $list);
		$this->display();
	}

	public function add(){
		if(!IS_POST){
			$this->error('access denied!');
		}
		$_POST['add_time'] = time();
		$_POST['u_id'] = session('uid');
		$_POST['is_del'] = isset($_POST['is_del']) ? $_POST['is_del'] : 0;
		$db = M('Post');
		$tag = trim($_POST['tag']);
		if(isset($tag) && !empty($tag)){
			$tdb = M('Tags');
			$tag = str_replace('，', ',', $tag);
			$tags = explode(',', $tag);
			foreach ($tags as $key => $value) {
				$res = $tdb->where("tag_name = $value")->getField('id');
				if($res){
					$istIdArr[] = $res;
				}else{		
					$data['tag_name'] = $value;
					$res = $tdb->data($data)->add(); 
					$istIdArr[] = $res;	
				}			
			}			
		}
		if($db->create()){
			if($res = $db->add()){
				$tpdb = M('tagPost');
				foreach ($istIdArr as $value) {
					$data['p_id'] = $res;
					$data['t_id'] = $value;
					$tpdb->data($data)->add();
				}
				$this->success('发布成功！');
			}else{
				$this->error('发布失败！');
			}
		}else{
			$this->error($db->getError());
		}

	}

	public function editUi(){
		import('Class.Cate',APP_PATH);
		$list = M('Cate')->select();
        $this->cates = Cate::unlimitedForLevel($list);		
		$id = I('id',intval,0);
		$res = M('Post')->find($id);
		$this->assign('post', $res);
		$this->display();

	}

	public function edit(){
		$id = I('id',0,'intval');
		$db = M('Post');
		$data['title'] = $_POST['title'];
		$data['c_id'] = $_POST['c_id'];
		$data['sort'] = $_POST['sort'];
		$data['click'] = $_POST['click'];
		$data['keyword'] = $_POST['keyword'];
		$data['description'] = $_POST['description'];
		$data['tags'] = $_POST['tags'];
		$data['content'] = $_POST['content'];
		
		if($db->where("id = " . $id)->data($data)->save()){			
			$this->success('成功！');
		}else{
			$this->error('失败！');
		}
	}

	public function del(){
		if(!($this->isAjax())){
			$this->ajaxReturn('0','access deneid!',0);
		}
		$id = I('id',0,intval);
		$res = M('Post')->where("id = ".$id)->setField('is_del',1);
		if($res){
			$data['status'] = 1;
			$data['info'] = '删除成功！';
		}else{	
			$data['status'] = 0;
			$data['info'] = '删除失败！';		
		}				
		$this->ajaxReturn($data);
	}
	//编辑器上传图片
	 /*
     *   'url'      :'a.jpg',   //保存后的文件路径
     *   'title'    :'hello',   //文件描述，对图片来说在前端会添加到title属性上
     *   'original' :'b.jpg',   //原始文件名
     *   'state'    :'SUCCESS'  //上传状态，成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
     */
	public function upload(){
		import('ORG.NET.UploadFile');
		$config =   array(
	        'maxSize'           =>  -1,    // 上传文件的最大值
	        'autoSub'           =>  true,// 启用子目录保存文件
	        'subType'           =>  'date',// 子目录创建方式 可以使用hash date custom
	        'dateFormat'        =>  'Ym',
	        'savePath'			=>  './Upload/',
        );
		$upload = new UploadFile($config);
		if($upload->upload()){
			$info = $upload->getUploadFileInfo();
			echo json_encode(array(
					'url' => $info[0]['savename'],
					'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES),
					'oruginal' => $info[0]['name'],
					'state' => 'SUCCESS'
				));
		}else{
			echo json_encode(array(
					'state' => $upload->getErrorMsg(),
				));
		}
	}
}